//
//  DDCategoryViewController.m
//  Dingdong
//
//  Created by 程强 on 16/11/11.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDCategoryViewController.h"
#import "DDCategoryCollectionCell.h"
#import "DDHotSaleView.h"
#import "DDCategoryManager.h"
#import "DDParentCategoryTableCell.h"
#import "DDCategorySet.h"
#import "DDGoodsListViewController.h"
#import "DDSearchViewController.h"
#import "DDShoppintCartViewController.h"
#import "UIButton+Badge.h"

@interface DDCategoryViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *parentTable;
@property (weak, nonatomic) IBOutlet UICollectionView *categoryCollection;
@property (strong, nonatomic) UIButton *rightItemBtn;
@property (strong, nonatomic) DDCategoryManager *manager;
@property (strong, nonatomic) DDCategorySet *categoryAry;
@property (strong, nonatomic) DDCategoryObj *collectionCategoryObj;
@end

@implementation DDCategoryViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([SystemConfig shareInstance].isLogin) {
        [[SystemConfig shareInstance]updateCartCountSueesss:^(NSString *count) {
            self.rightItemBtn.badgeValue = count;
        }];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [DDCategoryManager new];
    [self layoutCategoryView];
}
- (void)layoutCategoryView
{
    [self fetchCategory];
    
    self.rightItemBtn = [[UIButton alloc] init];
    [_rightItemBtn setBackgroundImage:LoadImage(@"category_cart") forState:UIControlStateNormal];
    [_rightItemBtn setFrame:CGRectMake(0, 0, 22, 22)];
    _rightItemBtn.shouldHideBadgeAtZero = YES;
    [_rightItemBtn addTarget:self action:@selector(gotoCart) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:_rightItemBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UIView *searchBgView = [[UIView alloc]initWithFrame:CGRectMake(60, 7, ScreenWidth-120, 30)];
    searchBgView.layer.cornerRadius = 3.0;
    searchBgView.backgroundColor = RGBColor(238, 238, 238);
    self.navigationItem.titleView =searchBgView;
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBtn setFrame:CGRectMake(0, 0, searchBgView.bounds.size.width, searchBgView.bounds.size.height)];
    [searchBtn addTarget:self action:@selector(gotoSearch:) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setTitle:@"iPhone7" forState:UIControlStateNormal];
    [searchBtn setTitleColor:RGBColor(180, 180, 180) forState:UIControlStateNormal];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [searchBtn setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, searchBgView.bounds.size.width-130)];
    [searchBgView addSubview:searchBtn];
    
    UIImageView *searchIcon = [[UIImageView alloc]initWithFrame:CGRectMake(15, 5, 20, 20)];
    searchIcon.image = LoadImage(@"category_sousuo");
    [searchBgView addSubview:searchIcon];
}
- (void)gotoCart
{
    DDShoppintCartViewController *vc = StoryBoardViewController(@"Main", @"DDShoppintCartViewController");
    vc.vcType = DingDongCartControllerTypePush;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)gotoSearch:(UIButton *)btn
{
    DDSearchViewController *vc = [[DDSearchViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:NO];
}
#pragma mark --- 接口
- (void)fetchCategory
{
    weak_Self(self);
    [[self.manager getCategory:@(0)]subscribeNext:^(id x) {
        weakSelf.categoryAry = x;
        [weakSelf.parentTable reloadData];
        [weakSelf reloadCategoryInfo];
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (void)reloadCategoryInfo
{
    [self.parentTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    self.collectionCategoryObj = self.categoryAry.categorySet[0];
    [self.categoryCollection reloadData];
}
#pragma mark --- UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.categoryAry.categorySet.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"ParentCell";
    
    DDParentCategoryTableCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        cell = [[DDParentCategoryTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    UIView *backgroudView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    backgroudView.backgroundColor = RGBColor(244, 244, 244);
    cell.selectedBackgroundView = backgroudView;
    
    DDCategoryObj *obj = self.categoryAry.categorySet[indexPath.row];
    cell.categoryNameLbl.text = obj.name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.collectionCategoryObj = self.categoryAry.categorySet[indexPath.row];
    [self.categoryCollection reloadData];
}
#pragma mark --- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectionCategoryObj.children.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"CategoryCell";
    DDCategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:indentifier forIndexPath:indexPath];
    
    DDCategoryObj *obj = self.collectionCategoryObj.children[indexPath.row];
    [cell.categoryIcon sd_setImageWithURL:[NSURL URLWithString:obj.image] placeholderImage:nil];
    cell.categoryNameLbl.text = obj.name;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DDCategoryObj *obj = self.collectionCategoryObj.children[indexPath.row];
    DDGoodsListViewController *vc = StoryBoardViewController(@"Shop", @"DDGoodsListViewController");
    vc.category_id = obj.cat_id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark ---- UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (ScreenWidth-150)/3.0;
    CGFloat height = width+60;
    return (CGSize){width,height};
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.f;
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.f;
}


//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
//    return (CGSize){ScreenWidth,44};
//}
//
//
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//{
//    return (CGSize){ScreenWidth,22};
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
